<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View};
use think\exception\ValidateException;

use app\common\model\{WorkerBank, WorkerCate};
use app\common\validate\{WorkerCateValidate, WorkerBankValidate};

class WorkerController extends BaseController
{
    private $workerBankModel;
    public function __construct(WorkerBank $workerBankModel)
    {
        parent::initialize();
        $this->workerBankModel = $workerBankModel;
    }

    /** 渲染人工分类列表页面 */
    public function worker_cate_list()
    {
        return view();
    }

    /** 渲染人工分类添加页面 */
    public function worker_cate_add()
    {
        return view();
    }

    /** 渲染人工分类编辑页面 */
    public function worker_cate_edit()
    {
        $data = input('');

        try{
            validate(WorkerCateValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workerModel = new WorkerCate();

        $info = $workerModel->getFind(['id' => $data['id']]);

        View::assign('info', $info['data']);
        return view();
    }

    /** 人工分类数据列表 */
    public function worker_cate_data()
    {
        $data = input('get.');

        try{
            validate(WorkerCateValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['name'])){
                $where[] = ['name', 'like', '%' . $data['key']['name'] . '%'];
            }
        }

        $workerCateModel = new WorkerCate();

        /** 查询数据总条数 */
        $count = $workerCateModel->getNum($where)['data'];

        /** 查询列表 */
        $list = $workerCateModel->getList($where, 1, $data['page'], $data['limit'], '*', 'id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 人工分类添加 */
    public function worker_cate_add_data()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(WorkerCateValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workerCateModel = new WorkerCate();

        $flag = $workerCateModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    /** 人工分类修改 */
    public function worker_cate_edit_data()
    {
        $data = input('post.');

        try{
            validate(WorkerCateValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workerCateModel = new WorkerCate();

        $flag = $workerCateModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('修改成功');
    }

    /** 人工分类删除 */
    public function worker_cate_del()
    {
        $data = input('post.');

        try{
            validate(WorkerCateValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workerCateModel = new WorkerCate();

        $flag = $workerCateModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }



    /** 渲染人工列表页面 */
    public function worker_bank_list()
    {
        return view();
    }

    /** 渲染人工添加页面 */
    public function worker_bank_add()
    {
        $workerCateModel = new WorkerCate();

        $list = $workerCateModel->getList([], 0);

        View::assign('cate_list', $list['data']);
        return view();
    }

    /** 渲染人工编辑页面 */
    public function worker_bank_edit()
    {
        $data = input('');

        try{
            validate(WorkerBankValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workerCateModel = new WorkerCate();

        $info = $this->workerBankModel->getFind(['id' => $data['id']]);

        $cateList = $workerCateModel->getList([], 0);

        View::assign('cate_list', $cateList['data']);
        View::assign('info', $info['data']);
        return view();
    }

    /** 人工数据列表 */
    public function worker_bank_data()
    {
        $data = input('get.');

        try{
            validate(WorkerBankValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['name'])){
                $where[] = ['name', 'like', '%' . $data['key']['name'] . '%'];
            }
        }

        /** 查询数据总条数 */
        $count = $this->workerBankModel->getNum($where)['data'];

        /** 查询列表 */
        $list = $this->workerBankModel->getList($where, 1, $data['page'], $data['limit'], '*', 'id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 人工添加 */
    public function worker_bank_add_data()
    {
        $data = input('post.');

        try{
            validate(WorkerBankValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $flag = $this->workerBankModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    /** 人工修改 */
    public function worker_bank_edit_data()
    {
        $data = input('post.');

        try{
            validate(WorkerBankValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $flag = $this->workerBankModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('修改成功');
    }

    /** 人工删除 */
    public function worker_bank_del()
    {
        $data = input('post.');

        try{
            validate(WorkerBankValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $flag = $this->workerBankModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
